from rest_framework import serializers

from students.models import Class, Student
from system.models import Department
from adminuser.serializers import DepartmentSerializers


class ClassSerializer(serializers.ModelSerializer):
    school = DepartmentSerializers(read_only=True)  # 关联序列化器
    school_id = serializers.IntegerField(write_only=True)


    def validate_school_id(self, value):

        try:
            Department.objects.get(id=value)
        except:
            raise serializers.ValidationError("请求的school_id 不合法找不到对应的学院信息")
        else:
            return value

    class Meta:
        model = Class
        fields = '__all__'


class StudentSerializer(serializers.ModelSerializer):
    cls = ClassSerializer()  # 关联的序列化器

    class Meta:
        model = Student
        fields = '__all__'
